/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.saville.testhmc;

import com.saville.debug.*;
import com.saville.hmc.*;
import com.saville.msgcomp.*;

public class TestUnhandledMsgs extends ActiveHmc {

    public TestUnhandledMsgs(String name) {
        super(name, 3, 1);
        df = Log.VERBOSE;

        Log.d(df, "TestUnhandledMsgs: %s MyHsm() E", mName);
        State_Default s = new State_Default();
        states[0] = s;
        states[1] = s.new State_Ping().setParent(0);
        states[2] = s.new State_Pong().setParent(0);

        /*
         * C A R E F U L: code after the call to completeConstruction 
         * may be executed after MC_CMD_STARTED is delivered to the
         * initial state.
         */
        completeConstruction();
        Log.d(df, "TestUnhandledMsgs: %s TestHsmServer2() X", mName);
    }

    private class State_Default extends State {
        @Override
        public void enterState(Msg msg) {
            Log.d(df, "TestUnhandledMsgs: %s State_Default.enterState", mName);
        }

        @Override
        public boolean doState(Msg msg) {
            Log.d(df, "TestUnhandledMsgs: %s State_Default", mName);
            if (msg.cmdIsRequest())
                sendReplyMsg(msg, 0);
            return true;
        }

        @Override
        public void exitState(Msg msg) {
            Log.d(df, "TestUnhandledMsgs: %s State_Default.exitState", mName);
        }

        private class State_Ping extends State {
            @Override
            public void enterState(Msg msg) {
                Log.d(df, "TestUnhandledMsgs: %s State_Ping.enterState", mName);
            }

            @Override
            public boolean doState(Msg msg) {
                Log.d(df, "TestUnhandledMsgs: %s State_Ping", mName);
                transitionTo(2);
                return false; // Test that base unhandled messages works
            }

            @Override
            public void exitState(Msg msg) {
                Log.d(df, "TestUnhandledMsgs: %s State_Ping.exitState", mName);
            }
        }

        private class State_Pong extends State {
            @Override
            public void enterState(Msg msg) {
                Log.d(df, "TestUnhandledMsgs: %s State_Pong.enterState", mName);
            }

            @Override
            public boolean doState(Msg msg) {
                Log.d(df, "TestUnhandledMsgs: %s State_Pong", mName);
                transitionTo(1);
                return false; // Test that base unhandled messages works
            }

            @Override
            public void exitState(Msg msg) {
                Log.d(df, "TestUnhandledMsgs: %s State_Pong.exitState", mName);
            }
        }
    }
}
